<template>
	<hyb-page title="地址列表">
		<!-- 请开始您的开发 ~ -->
		<template v-slot:body>
			<view class="gui-padding gui-bg-white">
				<view class="gui-margin-top gui-flex gui-row gui-space-between gui-align-items-center">
					<text class="gui-h5 gui-color-gray">常用地址</text>
					<view @tap="addAddress()" hover-class="gui-tap">
						<text class="gui-block gui-primary-color gui-text">+ 添加地址</text>
					</view>
				</view>
				<view class="gui-margin-top gui-flex gui-justify-content-end">

					<!-- <view @tap="copyAddress()" hover-class="gui-tap">
						<text class="gui-block gui-primary-color gui-text">+ 复制其他客户地址</text>
					</view> -->
				</view>
				<view class="gui-list gui-margin-top">
					<view class="gui-list-items" @tap="addAddress(item.d_id)" v-for="(item, index) in list"
						:key="index">

						<gui-radio @change="checkAddress(index)" :checked="checkedIndex==index" v-if="flag==1">
						</gui-radio>
						<view class="gui-list-body gui-border-b">
							<view class="gui-list-title">
								<text class="gui-list-title-text gui-color-black">{{item.d_uname}}</text>
								<text class="gui-list-title-desc gui-color-gray">{{item.d_phone}}</text>
							</view>
							<text class="gui-list-body-desc gui-color-gray">{{item.d_xxaddress}}</text>
						</view>
						<text class="gui-list-arrow-right gui-icons gui-color-gray">&#xe601;</text>
					</view>
				</view>


				<hyb-button v-if="flag==1" label="确定" @tap="confirm"></hyb-button>

				<!-- <view v-if="flag==1" class="gui-dark-bg-level-3 gui-padding gui-margin-top">
					<button type="default" class="gui-button gui-bg-primary gui-noborder" @tap="confirm"
						style="border-radius:50rpx;">
						<text class="gui-color-white gui-button-text">确定</text>
					</button>
				</view> -->
			</view>
		</template>
	</hyb-page>
</template>

<script>
	export default {
		data() {
			return {
				flag: 0,
				list: [],
				checkedIndex: 0,
				tag: "address"
			}
		},

		onShow() {
			this.query()
		},
		onLoad(options) {
			this.flag = options.flag || 0;
			this.tag = options.tag;
		},
		methods: {

			query() {
				uni.gPost("address/list").then((list) => {
					this.list = list;
				})
			},
			addAddress(id) {



				if (id) {

					uni.showActionSheet({
						itemList: ["修改", '删除'],
						success: (res) => {
							if (res.tapIndex == 0) {
								uni.gJs.navigate("/hyb_o2o/address/add?id=" + id);
							}
							if (res.tapIndex == 1) {
								uni.showModal({
									title: "确认删除该地址?",
									success: (res) => {
										if (res.confirm) {
											uni.gPost("address/remove", {
												id: id
											}).then((list) => {
												this.query();
											})
										}
									}
								})
							}
						}
					})



				} else {
					uni.gJs.navigate("/hyb_o2o/address/add");
				}
			},
			copyAddress() {
				uni.$once("choose_user", (user) => {
					uni.gPost("address/default", {
						u_id: user.u_id
					}).then((address) => {
						if (address) {
							address['d_id'] = 0;
							address['d_checked'] = 1
							uni.gPost("address/save", address).then((info) => {
								this.query()
							})
						}else{
							uni.showToast({
								title:"该用户没有设置默认地址",
								icon:'none'
							})
						}
					})
				})
				// uni.navigateTo({
				// 	url: '/hyb_o2o/user/list/list?tag=1'
				// })
			},
			checkAddress(index) {
				this.checkedIndex = index;

			},
			confirm() {
				uni.$emit(this.tag, this.list[this.checkedIndex]);
				uni.gJs.back();
			}
		}
	}
</script>

<style>

</style>